﻿using ICSharpCode.AvalonEdit.Highlighting;
using RoslynPad.Editor;
using System.Collections.Immutable;
using System.Windows.Media;

namespace AiMoWindows.Windows.Themes
{
    class DarkHighlightColors : IClassificationHighlightColors
    {
        private readonly Lazy<ImmutableDictionary<string, HighlightingColor>> _map;
        public HighlightingColor DefaultBrush { get; protected set; } = new HighlightingColor
        {
#if !RELEASE
            Foreground = new SimpleHighlightingBrush(Colors.Red)
#else
            Foreground = new SimpleHighlightingBrush(Colors.White)
#endif
        };

        public HighlightingColor TypeClassBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(78, 201, 176))
        };
        public HighlightingColor TypeStructBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(105, 210, 123))
        };
        public HighlightingColor TypeEnumBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(157, 226, 122))
        };

        public HighlightingColor MethodBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(233, 233, 171)),
            //FontWeight = FontWeights.DemiBold,
        };

        public HighlightingColor ParameterBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(156, 220, 254))
        };


        public HighlightingColor CommentBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(93, 150, 96))
        };


        public HighlightingColor XmlCommentBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Colors.Gray)
        };


        public HighlightingColor KeywordBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(86, 156, 214))
        };


        public HighlightingColor PreprocessorKeywordBrush { get; protected set; } = new HighlightingColor
        {

            Foreground = new SimpleHighlightingBrush(Colors.Gold)
        };


        public HighlightingColor StringBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(214, 157, 133))
        };


        public HighlightingColor BraceMatchingBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Colors.White),
            Background = new SimpleHighlightingBrush(Color.FromArgb(150, 50, 100, 255))
        };


        public HighlightingColor StaticSymbolBrush { get; protected set; } = new HighlightingColor
        {
            //  RhinoDoc."ActiveDoc".Layers;
            // Color."FromArgb"(random.Next(256), random.Next(256), random.Next(256));
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(255, 252, 168)),
            //FontWeight = FontWeights.Bold
        };
        public HighlightingColor PunctuationBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Colors.White),
        };

        public HighlightingColor NameSpaceBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(220, 220, 220)),
        };
        public HighlightingColor OperatorBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Colors.White),
        };
        public HighlightingColor FieldBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Colors.White),
        };
        public HighlightingColor NumberBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(225, 232, 178)),
        };
        public HighlightingColor LocalNameBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(156, 220, 254)),
        };
        public HighlightingColor PropertyNameBrush { get; protected set; } = new HighlightingColor
        {
            // layer."Name" = randomName;
            // int "Temp" {get;}
            Foreground = new SimpleHighlightingBrush(Colors.White),
        };
        public HighlightingColor NameIdentifierBrush { get; protected set; } = new HighlightingColor
        {
            // 不确定的颜色
            Foreground = new SimpleHighlightingBrush(Color.FromRgb(159, 175, 191)),
        };
        public HighlightingColor EnumMemberNameBrush { get; protected set; } = new HighlightingColor
        {
            // enum member name
            Foreground = new SimpleHighlightingBrush(Colors.White),
        };
        public HighlightingColor ConstantNameBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Colors.White),
        };
        public HighlightingColor ExcludedCodeBrush { get; protected set; } = new HighlightingColor
        {
            Foreground = new SimpleHighlightingBrush(Colors.Cyan),
        };

        public HighlightingColor SelectedTextMatchingBrush { get; protected set; } = new HighlightingColor
        {
            Background = new SimpleHighlightingBrush(Color.FromArgb(100, 200, 200, 200)),
        };

        public DarkHighlightColors()
        {
            _map = new Lazy<ImmutableDictionary<string, HighlightingColor>>(() => new Dictionary<string, HighlightingColor>
            {
                ["namespace name"] = AsFrozen(NameSpaceBrush),
                ["operator"] = AsFrozen(OperatorBrush),
                ["operator - overloaded"] = AsFrozen(OperatorBrush),
                ["number"] = AsFrozen(NumberBrush),
                ["field name"] = AsFrozen(FieldBrush),
                ["local name"] = AsFrozen(LocalNameBrush),
                ["property name"] = AsFrozen(PropertyNameBrush),
                ["identifier"] = AsFrozen(NameIdentifierBrush),
                ["enum member name"] = AsFrozen(EnumMemberNameBrush),
                ["constant name"] = AsFrozen(ConstantNameBrush),
                ["excluded code"] = AsFrozen(ExcludedCodeBrush),
                ["selected text matching"] = AsFrozen(SelectedTextMatchingBrush),

                ["class name"] = AsFrozen(TypeClassBrush),
                ["record class name"] = AsFrozen(TypeClassBrush),
                ["record struct name"] = AsFrozen(TypeStructBrush),
                ["struct name"] = AsFrozen(TypeStructBrush),
                ["interface name"] = AsFrozen(TypeEnumBrush),
                ["delegate name"] = AsFrozen(TypeClassBrush),
                ["enum name"] = AsFrozen(TypeEnumBrush),
                ["module name"] = AsFrozen(TypeClassBrush),
                ["type parameter name"] = AsFrozen(TypeClassBrush),
                ["method name"] = AsFrozen(MethodBrush),
                ["extension method name"] = AsFrozen(MethodBrush),
                ["parameter name"] = AsFrozen(ParameterBrush),
                ["comment"] = AsFrozen(CommentBrush),
                ["static symbol"] = AsFrozen(StaticSymbolBrush),
                ["xml doc comment - attribute name"] = AsFrozen(XmlCommentBrush),
                ["xml doc comment - attribute quotes"] = AsFrozen(XmlCommentBrush),
                ["xml doc comment - attribute value"] = AsFrozen(XmlCommentBrush),
                ["xml doc comment - cdata section"] = AsFrozen(XmlCommentBrush),
                ["xml doc comment - comment"] = AsFrozen(XmlCommentBrush),
                ["xml doc comment - delimiter"] = AsFrozen(XmlCommentBrush),
                ["xml doc comment - entity reference"] = AsFrozen(XmlCommentBrush),
                ["xml doc comment - name"] = AsFrozen(XmlCommentBrush),
                ["xml doc comment - processing instruction"] = AsFrozen(XmlCommentBrush),
                ["xml doc comment - text"] = AsFrozen(CommentBrush),
                ["keyword"] = AsFrozen(KeywordBrush),
                ["keyword - control"] = AsFrozen(KeywordBrush),
                ["preprocessor keyword"] = AsFrozen(PreprocessorKeywordBrush),
                ["string"] = AsFrozen(StringBrush),
                ["string - verbatim"] = AsFrozen(StringBrush),
                ["brace matching"] = AsFrozen(BraceMatchingBrush),
                ["punctuation"] = AsFrozen(PunctuationBrush),
            }.ToImmutableDictionary());
        }
        ImmutableDictionary<string, HighlightingColor> GetOrCreateMap()
        {

            return _map.Value;
        }

        public HighlightingColor GetBrush(string classificationTypeName)
        {
            GetOrCreateMap().TryGetValue(classificationTypeName, out HighlightingColor? value);
#if !RELEASE
            if (value == null)
            {
                System.Diagnostics.Debug.WriteLine("Found_unknown_type_name:   " + classificationTypeName);
            }
#endif
            return value ?? AsFrozen(DefaultBrush);
        }

        private static HighlightingColor AsFrozen(HighlightingColor color)
        {
            if (!color.IsFrozen)
            {
                color.Freeze();
            }

            return color;
        }
    }
}
